﻿namespace pictool
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var logger = new ttlib.ConsoleLogger();

            if (args.Length == 0 || TryGetCmd(args[0], out var cmd) == false)
            {
                Console.WriteLine("use 1: Pictool pack [path:string] [texsize:int]");
                Console.WriteLine("use 2: Pictool split [texfilename:string] [cutx:int] [cuty:int] [cutborder:bool]");
                Console.WriteLine("use 3: Pictool unpack [texfilename:string]");
                return;
            }
            try
            {
                if (cmd == "pack")
                {
                    int texsize = 2048;
                    var path = args[1];
                    if (args.Length > 2)
                    {
                        if (int.TryParse(args[2], out var nsize))
                        {
                            texsize = nsize;
                        }
                    }


                    TexPacker.PackTool.Pack(logger, path, texsize);
                }
                if (cmd == "split")
                {
                    var filename = args[1];
                    int cutx = int.Parse(args[2]);
                    int cuty = int.Parse(args[3]);
                    bool cutborder = false;
                    if (args.Length > 4)
                    {
                        if (TryGetBool(args[4], out var ncut))
                        {
                            cutborder = ncut;
                        }
                    }
                    TexPacker.PackTool.CutTex(logger, filename, cutx, cuty, cutborder);
                }
                if (cmd == "unpack")
                {
                    var filename = args[1];
                 
                    TexPacker.PackTool.UnPack(logger, filename);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error("错误，检查输入");
            }
        }
        static bool TryGetBool(string arg, out bool flag)
        {
            var l = arg.ToLower();
            if (l == "1" || l == "true")
            {
                flag = true;
                return true;
            }
            else if (l == "0" || l == "false")
            {
                flag = false;
                return false;
            }
            flag = false;
            return false;
        }
        static bool TryGetCmd(string arg, out string cmd)
        {
            if (arg.ToLower() == "pack")
            {
                cmd = "pack";
                return true;
            }
            else if (arg.ToLower() == "unpack")
            {
                cmd = "unpack";
                return true;
            }
            else if (arg.ToLower() == "split")
            {
                cmd = "split";
                return true;
            }
            else
            {
                cmd = null;
                return false;
            }
        }
    }
}